/*
 * @(#)RSocketApplication.java 2021/1/22
 *
 * Copyright (c) 2010 by rayootech.com. All rights reserved.
 */
package com.nuctech.rsocket;

import com.google.common.collect.Maps;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * RSocket启动器
 *
 * @author lilu
 * @date 2021/1/22 2:06 下午
 * @since 1.0.0
 */
@Slf4j
@SpringBootApplication
public class RSocketApplication {

	private static String APP_NAME = "";
	private static String APP_DESC = "";
	private static String APP_VERS = "";

	public static void main(String[] args) throws UnknownHostException {
		ConfigurableApplicationContext application = SpringApplication.run(RSocketApplication.class, args);

		Environment env = application.getEnvironment();
		String ip = InetAddress.getLocalHost().getHostAddress();
		String port = env.getProperty("server.port");
		String path = env.getProperty("server.servlet.context-path");

		APP_NAME = env.getProperty("spring.application.name");
		APP_DESC = env.getProperty("spring.application.description");
		APP_VERS = env.getProperty("spring.application.version");

		if (StrUtil.isBlank(path)) {
			path = "";
		}
		log.info("\n----------------------------------------------------------\n" +
				"\t项目启动成功 *^_^* \n" +
				"----------------------------------------------------------\n\t" +
				"Application  is running! Access URLs:\n\t" +
				"Local访问网址: \t\thttp://localhost:" + port + path + "\n\t" +
				"External访问网址: \thttp://" + ip + ":" + port + path + "\n" +
				"----------------------------------------------------------");
	}

	/**
	 * 主页 服务接口控制器
	 *
	 * @author lilu
	 * @date 2020-03-30
	 * @since 1.0.0
	 */
	@RestController
	static class IndexController {

		@RequestMapping({"/", "/index"})
		public Map<String, Object> index(HttpServletRequest request) {
			Map<String, Object> info = Maps.newLinkedHashMap();

			info.put("系统名称", APP_NAME);
			info.put("系统描述", APP_DESC);
			info.put("系统版本", APP_VERS);
			info.put("系统时间", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日(EEEE)HH:mm:ss.SSS")));

			return info;
		}

	}

}
